package edu.washington.cs.pomdb.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Semaphore;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import edu.washington.cs.pomdb.ExpCtrl.Actions;
import edu.washington.cs.pomdb.ExpCtrl.Observations;

public class ObservationFrame extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 201202281047L;

	private Semaphore semaphore;
	private Actions a;
	private JLabel infoLabel;
	private JButton [] actionButtons;

	private String userName = "guest";
	private double reward = 0;
	private double radius = 320;
	private double x = 0;
	private double y = 0;
	private Observations observ;
	private JPanel contentPane;


	public void setCircle(double x, double y, double r){
		this.radius = r;
		this.x = x;
		this.y = y;
	}

	public void setUserName(String s){
		userName = s;
	}

	public void setReward(double r){
		reward = r;
	}



	public Actions getAction(){
		try {
			semaphore.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;
	}



	public void createAndShowGUI(){
		infoLabel = new JLabel();
		infoLabel.setText(String.format("User Name: %s\t. Reward in this trial: %.2f",
				userName,reward));

		contentPane = (JPanel)this.getContentPane();
		contentPane.setLayout(null);
		contentPane.setBorder(BorderFactory.createEtchedBorder());
		contentPane.setBackground(new Color(204, 204, 204));
		
		
		GUI.addComponent(contentPane, infoLabel, 100,20,400,20);
		
		ObservationPanel op = new ObservationPanel(observ);
		op.setCircle(x,y,radius);
		GUI.addComponent(contentPane, op, 90, 50, (int)radius, (int)radius);

		actionButtons = new JButton[Actions.values().length];

		for(int i = 0; i < Actions.values().length; i++){
			//TODO button shows color
			actionButtons[i] = new JButton();
			actionButtons[i].setBackground(Actions.values()[i].getColor());
			actionButtons[i].setText(Actions.values()[i].toString());
			actionButtons[i].setOpaque(true);
			actionButtons[i].addActionListener(this);
			actionButtons[i].setToolTipText(String.format("Click this button to selct action %s", Actions.values()[i].toString()));
			actionButtons[i].setActionCommand(Actions.values()[i].toString());
			GUI.addComponent(contentPane, actionButtons[i], 100 + i * 100 ,400, 100,50);
		}
	
		this.setTitle("POMDB");	
		this.pack();
		this.setLocation(new Point(200, 200));
		this.setSize(new Dimension(500, 500));
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setResizable(true);
		this.setVisible(true);	
	}

	public ObservationFrame(Observations o, String s, double r){
		super();
		this.observ = o;
		this.userName = s;
		this.reward  = r;
		semaphore = new Semaphore(0);
		try {
			javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					createAndShowGUI();
				}
			});
		} catch (Exception e) { 
			System.err.println("ObservationFrame.createGUI didn't successfully complete");
			e.printStackTrace();
		}

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		for(Actions a1 : Actions.values()){
			if(a1.toString().equals(e.getActionCommand()))
				a = a1;
		}
		semaphore.release();
		this.dispose();
	}
}